package com.example.alibabanacosdiscoveryclient01.config.ai;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingModel;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
//import org.springframework.ai.vectorstore.RedisVectorStore;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPooled;

import java.util.Arrays;

@Configuration
public class MyVectorStoreConfig {
    @Value("${spring.ai.dashscope.api-key}")
    private String aiApiKey;
    @Value("${spring.data.redis.host}")
    private String host;
    @Value("${spring.data.redis.port}")
    private Integer port;
    @Value("${spring.data.redis.password}")
    private String password;
    @Bean
    public EmbeddingModel embeddingModel(){
     return  new DashScopeEmbeddingModel(new DashScopeApi(aiApiKey));
 }
    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel) {
        JedisPooled jedisPooled = new JedisPooled(host, port, null, password);
//        RedisVectorStore.RedisVectorStoreConfig config = RedisVectorStore.RedisVectorStoreConfig.builder()
////                .withMetadataFields(
////                        RedisVectorStore.MetadataField.text("content"),
////                        RedisVectorStore.MetadataField.tag("file_name"),
////                        RedisVectorStore.MetadataField.numeric("page_number")
////                        )
//                .withPrefix("embedding-ai:")
//                .withIndexName("my-vector")
//                .build();
//
//        RedisVectorStore redisVectorStore=new RedisVectorStore(config,embeddingModel,
//                jedisPooled,
//                true,
//                null,
//                null,
//                new TokenCountBatchingStrategy());
        RedisVectorStore redisVectorStore=RedisVectorStore.builder(jedisPooled,embeddingModel)
                .initializeSchema(true)
                .indexName("my-vector")
                .prefix("embedding-ai:")
                .metadataFields(Arrays.asList(
                        RedisVectorStore.MetadataField.numeric("year"),
                        RedisVectorStore.MetadataField.numeric("month"),
                        RedisVectorStore.MetadataField.text("content")
                        ))
                .build();
        return  redisVectorStore;
    }
}
